/*
 *
 * screen.c: simplify the usage of SDL2 (Simple DirectMedia Layer) library 
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "screen.h"

/* Main screen for displaying scene */
SDL_Surface *screen;
SDL_Window *window;
SDL_Renderer *renderer;		/* canvas */

/* TTF font to draw text */
TTF_Font *font = NULL;		/* font */
SDL_Texture *textTexture = NULL; /* font texture */
SDL_Color textColor = { 0,0,0 }; /* font color */
int textSize = DEFAULT_PTSIZE;   /* font size */

/*
 * Screen properties. These variables are immutable after calling screen_init()
 */

/* Screen width and height. */
int screen_width;
int screen_height;

/* event handler */
SDL_Event event;

/* Screen initialization.
 * Before calling this function, screen properties must be
 * set. In the initializing process, those variables may be reset and they are
 * never changed again during run time.
 */
int screen_init(int w, int h)
{
	int flags = 0;

	if(SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
		return -1;
	}

	screen_width = w;
	screen_height = h;

	window = SDL_CreateWindow("World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
			screen_width, screen_height, flags);
	if(window == NULL) {
		fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
		SDL_Quit();
		return -2;
	}

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
	if(renderer == NULL) {
		fprintf(stderr, "Couldn't get an appropriate renderer: %s\n", SDL_GetError());
		SDL_Quit();
		return -3;
	}

	screen = SDL_CreateRGBSurface(0, screen_width, screen_height, 32,
			0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);

	/* Initialize the TTF library */
	if (TTF_Init() < 0) {
		fprintf(stderr, "Couldn't initialize TTF: %s\n", SDL_GetError());
		SDL_Quit();
		return -4;
	}
	//Open the font
	font = TTF_OpenFont("msyh.ttf", textSize);
	if (font == NULL)
	{
		fprintf(stderr, "Failed to load msyh.ttf! SDL_ttf Error: %s\n", TTF_GetError());
		TTF_Quit();
		SDL_Quit();
		return -5;
	}

	SDL_ShowCursor(SDL_DISABLE);
	atexit(screen_quit);

	return 0;
}

/* screen_quit() is called automatically when the process terminates.
 * This function is registerd in screen_init() by callint atexit(). */
void screen_quit(void)
{
	SDL_ShowCursor(SDL_ENABLE);
	if(font)
		TTF_CloseFont(font);
	TTF_Quit();
	SDL_Quit();
}

/* Set the caption. Typically, captions is displayed in the title bar of
 * main screen when running in a windowing system. */
void screen_setcaption(const char *str)
{
	SDL_SetWindowTitle(window, str);
}

/* Fill the screen with the color. */
void screen_clear(int r, int g, int b, int a)
{
	SDL_SetRenderDrawColor(renderer, r, g, b, a);
	SDL_RenderClear(renderer);
//	SDL_RenderPresent(renderer);
}

/* Updates the screen, flag > 1 to copy mainTex */
int screen_update()
{
	SDL_RenderPresent(renderer);
	return 0;
}

void settextcolor(int r, int g, int b)
{
	textColor.r = r;
	textColor.g = g;
	textColor.b = b;
}

void settextsize(int s)
{
	textSize = s;
}

bool outtextxy(const char* str, int x, int y)
{
	int w, h;
	//Free texture if it exists
	if (textTexture != NULL)
	{
		SDL_DestroyTexture(textTexture);
		textTexture = NULL;
	}

	//Render text surface
	SDL_Surface* textSurface = TTF_RenderUTF8_Solid(font, str, textColor);
	if (textSurface == NULL)
	{
		fprintf(stderr, "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
		return false;
	}
	else
	{
		//Create texture from surface pixels
		textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
		if (textTexture == NULL)
		{
			fprintf(stderr, "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
			return false;
		}
		else
		{
			//Get image dimensions
			w = textSurface->w;
			h = textSurface->h;
		}

		//Get rid of old surface
		SDL_FreeSurface(textSurface);
	}

	//Set rendering space and render to screen
	SDL_Rect renderQuad = { x, y, w, h };
	//Render to screen
	//SDL_RenderCopyEx(gRenderer, mTexture, clip, &renderQuad, angle, center, flip);
	SDL_RenderCopy(renderer, textTexture, NULL, &renderQuad);

	//Return success
	return true;
}

